package com.chen.tliaswebtest.service.impl;

import com.chen.tliaswebtest.exception.TliasException;
import com.chen.tliaswebtest.mapper.EmpMapper;
import com.chen.tliaswebtest.mapper.StudentMapper;
import com.chen.tliaswebtest.pojo.ClazzReport;
import com.chen.tliaswebtest.pojo.DegreeReport;
import com.chen.tliaswebtest.pojo.JobReport;
import com.chen.tliaswebtest.pojo.JobReportDTO;
import com.chen.tliaswebtest.service.DataReportService;
import com.chen.tliaswebtest.tilasenum.DegreeEnum;
import com.chen.tliaswebtest.tilasenum.JobEnum;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/7/25
 * 员工数据统计逻辑层
 */
@Service
// 这里都是查询逻辑，所以不需要事务
// @Transactional
public class DataReportServiceImpl implements DataReportService {
    private final EmpMapper empMapper;
    private final StudentMapper studentMapper;


    public DataReportServiceImpl(EmpMapper empMapper, StudentMapper studentMapper) {
        this.empMapper = empMapper;
        this.studentMapper = studentMapper;
    }

    @Override
    public List<Map<String, Integer>> getEmpGenderData() {
        // 这里使用map集合来保存数据
        return empMapper.countByGender();
    }

    @Override
    public JobReportDTO getEmpJobData() {
        // 这里使用List集合存储数据
        List<JobReport> reportList = empMapper.countByJob();
        List<String> jobList = reportList.stream().map(s -> JobEnum.getNameByCode(s.getJob())).toList();
        List<Integer> dataList = reportList.stream().map(JobReport::getCount).toList();
        return JobReportDTO.builder().jobList(jobList).dataList(dataList).build();
    }

    @Override
    public List<DegreeReport> getStudentDegreeData() {
        // 这里使用List集合存储数据
        List<DegreeReport> reportList = studentMapper.coutnByDegree();
        reportList.forEach(s -> {
            Integer code = null;
            try {
                code = Integer.valueOf(s.getName());
            } catch (NumberFormatException e) {
                throw new TliasException("学历转换失败，学历查询结果异常！");
            }
            s.setName(DegreeEnum.getNameByCode(code));
        });
        return reportList;
    }

    @Override
    public ClazzReport getStudentCountByClazzData() {
        // 这里使用map集合来存储数据
        List<Map<String, Object>> reportList = studentMapper.countByClazz();
        List<String> clazzList = reportList.stream()
                .map(map -> String.valueOf(map.get("clazzName"))).toList();
        List<Long> dataList = reportList.stream().map(map -> (Long) map.get("count")).toList();
        return ClazzReport.builder().clazzList(clazzList).dataList(dataList).build();
    }
}
